package com.example.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import com.example.service.MessageService;

@RestController
@CrossOrigin
public class SSEController {

    @Autowired
    private MessageService messageService;

    @GetMapping("/subscribe/{clientId}")
    public SseEmitter subscribe(@PathVariable String clientId) {
        return messageService.createEmitter(clientId);
    }

    @PostMapping("/send/{targetClientId}")
    public void sendToClient(@PathVariable String targetClientId, @RequestBody String message) {
        messageService.sendMessage(targetClientId, message);
    }

    @PostMapping("/broadcast")
    public void broadcast(@RequestBody String message) {
        messageService.broadcast(message);
    }
}
